package cn.wang.web.cargo.export;


import cn.wang.entity.cargo.export.Export;
import cn.wang.entity.cargo.export.ExportProduct;
import cn.wang.service.cargo.ContractService;
import cn.wang.service.cargo.export.ExportService;
import cn.wang.utils.FastJsonUtil;
import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;
import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.webservice.EpService;
import com.yaorange.webservice.Exception_Exception;
import com.yaorange.webservice.WsExport;
import com.yaorange.webservice.WsExportProduct;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Controller
public class ExportAction extends BaseAction implements ModelDriven<Export> {
    private Pagination page = new Pagination();
    private List<Export> exportList;
    private Export model = new Export();
    //修改运单货物属性
    private String[] mr_id;
    private String[] mr_changed;
    private Integer[] mr_orderNo;
    private Integer[] mr_cnumber;
    private Double[] mr_grossWeight;
    private Double[] mr_netWeight;
    private Double[] mr_sizeLength;
    private Double[] mr_sizeWidth;
    private Double[] mr_sizeHeight;
    private Double[] mr_exPrice;
    private Double[] mr_tax;

    @Autowired
    private ExportService exportService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private EpService epService;

    //待报运合同查询
    public String contractList() {
        page = contractService.findPage(page.getPageNo(), page.getPageSize(), 1);
        page.setUrl("exportAction_list");
        push(page);
        return "contractList";
    }


    //显示出口报运单
    public String list() {
        page = exportService.findPage(page.getPageNo(), page.getPageSize(), 0);
        page.setUrl("exportAction_list");
        push(page);
        return "list";
    }
    //查询所有部门
    public String tocreate() {

        return "tocreate";
    }

    //查寻单个部门信息
    public String toview() {
        model = exportService.findById(model.getId());
        push(model);
        return "toview";
    }

    //添加部门
    public String insert() {
        exportService.save(model);
        return contractList();

    }

    //更新 先查询
    public String toupdate() {
        model = exportService.findById(model.getId());
        push(model);
        return "toupdate";
    }

    public String getExprotProductJson() {
        //   FastJsonUtil.write_json(ServletActionContext.getResponse(),"[{'哈哈':'哈哈'}]");
        //根据id查询报运货物
        model = exportService.findById(model.getId());
        Set<ExportProduct> setep = model.getExportProducts();
        //将set转为json
        String json = JSON.toJSONString(setep);
        FastJsonUtil.write_json(ServletActionContext.getResponse(), json);
        return null;
    }

    public String update() {
        exportService.update(model, mr_id, mr_changed, mr_grossWeight, mr_orderNo, mr_cnumber, mr_netWeight, mr_sizeLength, mr_sizeWidth, mr_sizeHeight, mr_exPrice, mr_tax);
        return "rlist";
    }

    //删除部门 多个部门删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete() {
        if (model.getId() == null) {
            return "rlist";
        }
        String[] ids = model.getId().split(", ");
        exportService.delete(ids);
        return "rlist";
    }
    //报运

    public String export() {
        //准备报运数据
        model = exportService.findById(model.getId());
        WsExport wsExport = new WsExport();

        //将数据复制到目标对象中
        try {
            BeanUtils.copyProperties(wsExport, model);
            wsExport.setExportId(model.getId());
            //复制产品信息
            Set<WsExportProduct> wsSet = new HashSet<>();
            Set<ExportProduct> Products = model.getExportProducts();

            for (ExportProduct cp : Products) {
                WsExportProduct wppt = new WsExportProduct();
                BeanUtils.copyProperties(wppt, cp);
                wppt.setExportProductId(cp.getId());
                wsSet.add(wppt);
            }
            wsExport.setProducts(wsSet);
            //2将数据转为json
            String json = FastJsonUtil.toJSONString(wsExport);
            String result = epService.exportE(json);
            System.out.println(result);
            //3修改报运单状态 1 为已上报
            model.setState(1);
            exportService.update(model);
        } catch ( IllegalAccessException e ) {
            e.printStackTrace();
        } catch ( InvocationTargetException e ) {
            e.printStackTrace();
        } catch ( Exception_Exception e ) {
            e.printStackTrace();
        }

        return "rlist";
    }

    public void setPage(Pagination page) {
        this.page = page;
    }

    public List<Export> getExportList() {
        return exportList;
    }

    @Override
    public Export getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }

    public void setMr_id(String[] mr_id) {
        this.mr_id = mr_id;
    }

    public void setMr_changed(String[] mr_changed) {
        this.mr_changed = mr_changed;
    }

    public void setMr_orderNo(Integer[] mr_orderNo) {
        this.mr_orderNo = mr_orderNo;
    }

    public void setMr_cnumber(Integer[] mr_cnumber) {
        this.mr_cnumber = mr_cnumber;
    }

    public void setMr_grossWeight(Double[] mr_grossWeight) {
        this.mr_grossWeight = mr_grossWeight;
    }

    public void setMr_netWeight(Double[] mr_netWeight) {
        this.mr_netWeight = mr_netWeight;
    }

    public void setMr_sizeLength(Double[] mr_sizeLength) {
        this.mr_sizeLength = mr_sizeLength;
    }

    public void setMr_sizeWidth(Double[] mr_sizeWidth) {
        this.mr_sizeWidth = mr_sizeWidth;
    }

    public void setMr_sizeHeight(Double[] mr_sizeHeight) {
        this.mr_sizeHeight = mr_sizeHeight;
    }

    public void setMr_exPrice(Double[] mr_exPrice) {
        this.mr_exPrice = mr_exPrice;
    }

    public void setMr_tax(Double[] mr_tax) {
        this.mr_tax = mr_tax;
    }
}
